import 'dotenv/config'
import mysql from 'mysql2/promise'

const dbConfig = {
  host: process.env.DB_HOST || '127.0.0.1',
  port: Number(process.env.DB_PORT || 3306),
  user: process.env.DB_USER || 'root',
  password: process.env.DB_PASSWORD || '123456',
  database: process.env.DB_NAME || 'music-min',
  connectionLimit: 10
}

const pool = mysql.createPool(dbConfig)

async function addMusicData() {
  try {
    // 确保 music 表存在
    await pool.query(`
      CREATE TABLE IF NOT EXISTS music (
          id INT AUTO_INCREMENT PRIMARY KEY,
          title VARCHAR(255) NOT NULL,
          artist VARCHAR(255),
          album VARCHAR(255),
          cover VARCHAR(255),
          src VARCHAR(255),
          duration INT,
          genre VARCHAR(255),
          lyrics TEXT,
          play_count INT DEFAULT 0,
          like_count INT DEFAULT 0,
          status TINYINT DEFAULT 1,
          created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      );
    `)
    console.log('music 表已存在或已创建。')

    // 清空现有数据
    await pool.query('DELETE FROM music')
    console.log('清空现有音乐数据。')

    // 添加测试音乐数据
    const musicData = [
      {
        title: '夜曲',
        artist: '周杰伦',
        album: '十一月的萧邦',
        cover: 'https://ai-public.mastergo.com/ai/img_res/85071e7663a90ba61a2700af928f3399.jpg',
        src: 'https://example.com/music/yequ.mp3',
        duration: 180,
        genre: '流行',
        lyrics: '一群嗜血的蚂蚁 被腐肉所吸引...',
        play_count: 0,
        like_count: 0
      },
      {
        title: '青花瓷',
        artist: '周杰伦',
        album: '我很忙',
        cover: 'https://ai-public.mastergo.com/ai/img_res/8404b597ab97241556ac1dce8d2c5ec2.jpg',
        src: 'https://example.com/music/qinghuaci.mp3',
        duration: 200,
        genre: '流行',
        lyrics: '素胚勾勒出青花笔锋浓转淡...',
        play_count: 0,
        like_count: 0
      },
      {
        title: '稻香',
        artist: '周杰伦',
        album: '魔杰座',
        cover: 'https://ai-public.mastergo.com/ai/img_res/346e24fb732344889d749f1d2fa20164.jpg',
        src: 'https://example.com/music/daoxiang.mp3',
        duration: 220,
        genre: '流行',
        lyrics: '对这个世界如果你有太多的抱怨...',
        play_count: 0,
        like_count: 0
      },
      {
        title: '告白气球',
        artist: '周杰伦',
        album: '周杰伦的床边故事',
        cover: 'https://ai-public.mastergo.com/ai/img_res/85071e7663a90ba61a2700af928f3399.jpg',
        src: 'https://example.com/music/gaobaiqiqiu.mp3',
        duration: 195,
        genre: '流行',
        lyrics: '塞纳河畔 左岸的咖啡...',
        play_count: 0,
        like_count: 0
      },
      {
        title: '听妈妈的话',
        artist: '周杰伦',
        album: '依然范特西',
        cover: 'https://ai-public.mastergo.com/ai/img_res/8404b597ab97241556ac1dce8d2c5ec2.jpg',
        src: 'https://example.com/music/tingmamadehua.mp3',
        duration: 240,
        genre: '流行',
        lyrics: '小朋友你是否有很多问号...',
        play_count: 0,
        like_count: 0
      }
    ]

    // 插入音乐数据
    for (const music of musicData) {
      const [result] = await pool.query(
        'INSERT INTO music (title, artist, album, cover, src, duration, genre, lyrics, play_count, like_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
        [music.title, music.artist, music.album, music.cover, music.src, music.duration, music.genre, music.lyrics, music.play_count, music.like_count]
      )
      console.log(`添加音乐: ${music.title} - ${music.artist} (ID: ${result.insertId})`)
    }

    // 查看所有音乐
    const [rows] = await pool.query('SELECT id, title, artist, album, duration FROM music ORDER BY id ASC')
    console.log('当前所有音乐:')
    rows.forEach(row => {
      console.log(`ID: ${row.id}, 标题: ${row.title}, 歌手: ${row.artist}, 专辑: ${row.album}, 时长: ${row.duration}秒`)
    })

  } catch (err) {
    console.error('添加音乐数据失败:', err)
  } finally {
    pool.end()
  }
}

addMusicData()

